//
//  JZHMessageViewController.m
//  极致荟
//
//  Created by mac on 14-4-8.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "JZHMessageViewController.h"
#import "JZHFrenListTableViewCell.h"
#import "JZHContactsViewController.h"
#import "JZHFrendPulisViewController.h"
#import "JZHDovViewController.h"
#import "JZHExpertViewController.h"
#import "JZHAddViewController.h"
#import "JZHHowLookMeViewController.h"
#import "JZHFuJinViewController.h"
#import "JZHDetailsViewController.h"
#import "OtherUsersDao.h"
#import "JZHChatViewController.h"
#import "FriendRequest.h"


@interface JZHMessageViewController ()
{
    NSMutableArray *arr;
    SouSuo *ss;
}
@property (weak, nonatomic) IBOutlet UILabel *requestNuber;

@end

@implementation JZHMessageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       
    }
    return self;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self requerstNubmer];
    [[JZHTabbarViewController shareInstance] showTabBar2];
     arr=[[NSMutableArray alloc]init];
    arr=[OtherUsersDao selectNoteMessage];
    [self.tableView reloadData];
}
- (void)viewDidLoad
{
   
    [super viewDidLoad];
     [self.requestNuber.layer setCornerRadius:7.5];
    self.tableView.tableHeaderView = self.headerView;
    [FriendRequest HttpFriendRequestcomYes:^(NSMutableArray *arr, NSString* rptime) {
        
         [self requerstNubmer];
    }];
    
    ss=[[SouSuo alloc] initPoint:CGPointMake(0, 0) superView:self.headerView concorell:self];
    [ss setSouDelegate:self];

   
    self.navigationItem.title=@"讯 息";
    //设置右侧导航按钮
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 44)];
    [rightButton addTarget:self action:@selector(rightDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setImage:[UIImage imageNamed:@"讯息_03@x2"] forState:UIControlStateNormal];
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightBarButton;
    
    //监听收到的消息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updowview:) name:SENDMESSAGE object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nuberfr:) name:SENDREQUEST object:nil];

}
- (void)rightDrawerButtonPress:(UIButton *)sender
{
    [UINavigationController Pus:self ToViewconeroller:[[JZHAddViewController alloc]init]];
}
- (IBAction)pusExce:(id)sender
{
    [UINavigationController Pus:self ToViewconeroller:[[JZHExpertViewController  alloc]init]];
   
}

- (void)updowview:(NSNotification *)not
{
    arr=[[NSMutableArray alloc]init];
    arr=[OtherUsersDao selectNoteMessage];
    [self.tableView reloadData];
}
- (void)nuberfr:(NSNotification *)not
{
    [self requerstNubmer];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 搜索框代理
///当搜索框文本变化时调用
- (void)SouSuoText:(NSString *)text
{
  [ss tableData: [OtherUsersDao selectUserName:text]] ;
}
///当点击键盘reuten建调用
- (void)SouSuoKeyRetunText:(NSString *)text
{
    [ss tableData: [OtherUsersDao selectUserName:text]] ;
}

- (void)selectTableRow:(id)data
{
    OtherUsers *ouser=data;
    [UINavigationController Pus:self ToViewconeroller:[[JZHDetailsViewController alloc] initOthUser:ouser]];
}
    
    

- (IBAction)pusfujin:(id)sender {
     [UINavigationController Pus:self ToViewconeroller:[[JZHFuJinViewController  alloc]init]];
   
}
#pragma mark ---tableview代理---
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return arr.count;
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str=@"adf";
    JZHFrenListTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str];
    if(!cell)
    {
        cell=[[[NSBundle mainBundle]loadNibNamed:@"JZHFrenListTableViewCell" owner:self options:nil] objectAtIndex:0];
    }
    
    
    [cell addCellData:arr[indexPath.row]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 67;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
 //[self.navigationController pushViewController:[[JZHDetailsViewController alloc]init] animated:YES];
    [UINavigationController Pus:self ToViewconeroller:[[JZHChatViewController alloc]initOuser:arr[indexPath.row] Recommend:nil] ];
//    
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    
    Note *note=[(OtherUsers *)arr[indexPath.row] note];
    note.content=@"";//设置为空代表被删除
    [OtherUsersDao NoteMessage:arr[indexPath.row]];
    [OtherUsersDao dorpNuber:arr[indexPath.row]];
    [arr removeObjectAtIndex:indexPath.row];
    
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]  withRowAnimation: UITableViewRowAnimationNone];
    arr=[OtherUsersDao selectNoteMessage];
    //[self.tableView reloadData];
}

- (IBAction)pusFrend:(id)sender {
   
    [UINavigationController Pus:self ToViewconeroller:[[JZHFrendPulisViewController alloc]init]];
}


-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return UITableViewCellEditingStyleDelete;
}
-(NSString*)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexpath
{
    return @"删除";
}
- (IBAction)pusContacList:(id)sender {
    
     [UINavigationController Pus:self ToViewconeroller:[[JZHContactsViewController alloc]initIsPusNote:nil farr:nil]];
}

- (IBAction)pusD:(id)sender
{
    [[[UIAlertView alloc] initWithTitle:@"提示" message:@"当前功能即将开放，请等待" delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil
     , nil] show];
    //[UINavigationController Pus:self ToViewconeroller:[[JZHDovViewController alloc]init]];
}

- (void)requerstNubmer
{
    if([FriendRequest selectRN]==0)
    {
        self.requestNuber.hidden=YES;
    }else
    {
        self.requestNuber.hidden=NO;
        self.requestNuber.text=[NSString stringWithFormat:@"%d",[FriendRequest selectRN]];
    }
    
}
#pragma mark ------搜索框代理------
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    searchBar.showsCancelButton=YES ;
    return YES;
}
@end
